package com.wyl.template.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wyl.template.aop.annotation.SysLogger;
import com.wyl.template.domain.common.PageResult;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.domain.excel.PatientExcel;
import com.wyl.template.domain.excel.SysLogExcel;
import com.wyl.template.domain.model.SysLogDO;
import com.wyl.template.domain.model.TPatientDO;
import com.wyl.template.domain.request.PatientPageRequest;
import com.wyl.template.domain.request.SysLogPageRequest;
import com.wyl.template.domain.vo.SysLogVO;
import com.wyl.template.enums.LoggerType;
import com.wyl.template.service.SysLogService;
import com.wyl.template.service.TPatientService;
import com.wyl.template.util.Des3New;
import com.wyl.template.util.DesensitizationUtil;
import com.wyl.template.util.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: wyl
 * @Date: 2021/7/5
 * @Description: 系统日志模块
 */
@SysLogger(group = "操作日志")
@RestController
@RequestMapping("/sysLog")
public class SysLogController extends BaseController<SysLogService, SysLogDO> {

    /**
     * 查询分页信息
     *
     * @param pageRequest
     * @return
     */
    @SysLogger(name = "分页查询", type = LoggerType.SELECT)
    @GetMapping("listLogs")
    public ResponseResult listLogs(SysLogPageRequest pageRequest) {
        ResponseResult<IPage<SysLogDO>> selectPage = this.selectPage(pageRequest);
        IPage pageInfo = selectPage.getData();
        List<SysLogDO> logList = pageInfo.getRecords();

        List<SysLogVO> sysLogVOList = logList.stream().map(log -> {
            SysLogVO logVO = new SysLogVO();
            BeanUtils.copyProperties(log, logVO);
            return logVO;
        }).collect(Collectors.toList());


        PageResult pageResult = new PageResult();
        BeanUtils.copyProperties(pageInfo, pageResult);
        pageResult.setRecords(sysLogVOList);
        return ResponseResult.success(pageResult);
    }

    /**
     * 导出日志信息
     *
     * @param pageRequest
     * @param response
     */
    @SysLogger(name = "导出日志信息", type = LoggerType.EXPORT)
    @GetMapping("/download")
    public void download(PatientPageRequest pageRequest, HttpServletResponse response) {
        ResponseResult<IPage<SysLogDO>> selectPage = this.selectPage(pageRequest);
        IPage pageInfo = selectPage.getData();
        List<SysLogDO> logList = pageInfo.getRecords();

        List<SysLogExcel> sysLogExcels = logList.stream().map(log -> {
            SysLogExcel logExcel = new SysLogExcel();
            BeanUtils.copyProperties(log, logExcel);
            return logExcel;
        }).collect(Collectors.toList());

        ExcelUtil.downloadExcel(response, "操作日志记录", sysLogExcels, SysLogExcel.class);
    }
}
